<form class="registed form_style" style="margin-top:0px;padding:0px">
	<div style="float: left;position: relative;display: block;width: 98%;height: 45px;padding: 5px;border-bottom:1px solid #e0e0e0">
		&emsp;<strong>Search</strong>&emsp;<input id="csearch" type="text" />
		&emsp;<strong>Date</strong>&emsp;
		<input type="text" id="datepick1" style="width:85px" maxlength="10" value="<?php echo date("Y-m-d"); ?>">
		-
        <input type="text" id="datepick2" style="width:85px" maxlength="10" value="<?php echo date("Y-m-d"); ?>"/>
		&emsp;
		<select id="cshow" style="width:100px;">
			<option value="0">All</option>
			<option value="1">Paid</option>
			<option value="2">UnPaid</option>
			<option value="3">Overdue</option>
			<option value="4">Delivered</option>
			<option value="5">Undelivered</option>
		</select>
	</div>
	<div style="position: relative;float: right;width: 120px;height: 380px;display: block;border-left:1px solid #e0e0e0;padding:10px;">
		<input class="cbutton" id="cpay" style="margin-top:10px;float:none;" type="button" value="Pay Cash" disabled/>
		<input class="cbutton" id="ccheque" style="margin-top:10px;float:none;" type="button" value="Pay Cheque"  disabled/>
		<hr style="display:block;margin:10px" />
		<input class="cbutton" id="clist" style="margin-top:10px;float:none;" type="button" value="Item List"  disabled/>
		<div>
			<input class="cbutton" id="cplist" style="margin-top:10px;float:none;" type="button" value="Payment List"  disabled/>
			<span id="pending" class="ui-corner-all ui-text-center ui-notif-count" style="display: none;">1</span>
		</div>
		<hr style="display:block;margin:10px" />
		<input class="cbutton" id="cdeliver" style="margin-top:10px;float:none;" type="button" value="Delivery"  disabled/>
		<input class="cbsutton" id="creturn" style="margin-top:10px;float:none;" type="button" value="Return Item"  disabled/>
		<hr style="display:block;margin:10px;margin-top:40px;" />
		<input style="margin-top:10px;float:none;" id="caddnew" type="button" value="Customers"/>
	</div>
	<div style="position: relative;float: right;width: 280px;height: 380px;display: block;border-left:1px solid #e0e0e0;padding:10px;">
		<span id="ccdate" style="display: inline-block;float: right;"></span>
		<strong>Receipt No :</strong><br />
		<input id="cor" type="text" style="width:92%" class="ui-text-center" disabled/><br />
		<strong>Name :</strong><br />
		<input id="cname" type="text" style="width:92%" class="ui-text-center" disabled/><br />
		<strong>Balance (&#8369;) :</strong><br />
		<input id="cbalance" type="text" style="width:92%" class="ui-text-center" value="0.00" disabled/><br />
		<hr style="margin:10px" />
		<strong>Due Date :</strong><br />
		<input id="cdue" type="text" style="width:92%" class="ui-text-center" disabled/><br />
		<strong class="adue" style="display:none;" >Amount Due (&#8369;) :</strong><br />
		<input id="cbdue" class="adue ui-text-center" type="text" style="width:92%;display:none;" value="0.00" disabled/><br />
	</div>
	
	<div id="paycash" style="position: absolute;background: #fff;width: 337px;height: 360px;z-index: 1000;border: 1px solid #e0e0e0;padding: 20px;left: 15px;top: 63px;float: none;display:none;">
		<strong style="float:left;clear: both;">Amount Due (&#8369;)</strong>
		<input type="text" style="width:55%;float:right;margin-top: -10px;" class="camountdue ui-text-center" value="0.00" disabled><br />	
		<strong style="float:left;clear: both;">Change (&#8369;)</strong>
		<input type="text" style="width:55%;float:right;margin-top: -10px;" class="cchange ui-text-center" value="0.00" disabled><br />
		<hr style="margin:10px;clear:both;" />
		<strong style="float:left;clear: both;">Cash (&#8369;)</strong><br />
		<input type="text" style="width:92%" class="ccash ui-text-center" value="0.00"><br />
		<hr style="margin:10px" />
		<input class="issuepayment cash" style="margin-top:10px;float:right;" type="button" value="Pay"/>
		<input style="margin-top:10px;float:right;" onclick="$('#paycash').fadeOut();" type="button" value="Cancel"/>
	</div>
	
	<div id="paycheque" style="position: absolute;background: #fff;width: 337px;height: 360px;z-index: 1000;border: 1px solid #e0e0e0;padding: 20px;left: 15px;top: 63px;float: none;display:none;">
		<strong style="float:left;clear: both;">Amount Due (&#8369;)</strong>
		<input type="text" style="width:55%;float:right;margin-top: -10px;" class="camountdue ui-text-center" value="0.00" disabled><br />	
		<strong style="float:left;clear: both;">Cash (&#8369;)</strong>
		<input type="text" style="width:55%;float:right;margin-top: -10px;" class="cchange ui-text-center" value="0.00" disabled><br />
		<hr style="margin:10px;clear:both;" />
		<strong style="float:left;clear: both;">Cheque Amount (&#8369;)</strong><br />
		<input type="text" style="width:92%" class="ccash ui-text-center" value="0.00"><br />
		<hr style="margin:10px" />
		<strong style="float:left;clear: both;">Cheque Info</strong><br />
		<table style="border:0px;margin:0px">
			<thead>
				<tr>
					<td class="ui-text-center">Check No.</td>
					<td></td>
					<td colspan="3" class="ui-text-center">R/T No.</td>
					<td></td>
					<td class="ui-text-center">Account No.</td>
				</tr>
			</thead>
			<tbody>
				<tr>
					<td style="width:80px"><input class="checkinput ui-text-center" type="text" maxlength="10" style="width: inherit;padding: 0px 5px 0px;height: 25px;" /></td>
					<td style="width: 5px;">-</td>
					<td style="width:40px"><input class="checkinput ui-text-center" type="text" maxlength="5" style="width: inherit;padding: 0px 5px 0px;height: 25px;" /></td>
					<td style="width: 5px;">-</td>
					<td style="width:35px"><input class="checkinput ui-text-center" type="text" maxlength="4" style="width: inherit;padding: 0px 5px 0px;height: 25px;" /></td>
					<td style="width: 5px;">-</td>
					<td style="width:95px"><input class="checkinput ui-text-center" type="text" maxlength="12" style="width: inherit;padding: 0px 5px 0px;height: 25px;" /></td>
				</tr>
				<tr>
					<td class="ui-text-center">Bank Name</td>
					<td colspan="6"><input id="cbank" class="ui-text-center" type="text" style="width: 90%;height:30px;padding: 0px 5px 0px;" /></td>
				</tr>
			</tbody>
		</table>
		<hr style="margin:0 10px 10px" />
		<input class="issuepayment check" style="float:right;" type="button" value="Pay"/>
		<input style="float:right;" onclick="$('#paycheque').fadeOut();" type="button" value="Cancel"/>
	</div>
	
	<div style="position: relative;float: left;height: 180px;width: 377px;display: block;">
		<table id="ctable" style="height:300px;">
			<thead>
				<tr style="display:block;position:relative;">
    				<th style="width:100px">OR</th>
    				<th style="width:200px" class="ui-text-center">Name</th>
    				<th style="width:75px">Balance</th>
				</tr>
			</thead>
			<tbody style="height: 300px;overflow: auto;display: block;width:inherit;padding-top: 6px;">
			</tbody>
		</table>
		<span style="color:green;text-indent:15px;font-weight:bold;display: inline-block;font-size: 11px;text-transform: uppercase"><span class="ui-corner-all ui-text-center ui-notif-count"  style="top:-10px;background:green;border:1px solid green">D</span>delivered</span>
		<span style="color:orange;text-indent:30px;font-weight:bold;display: inline-block;font-size: 11px;text-transform: uppercase"><span class="ui-corner-all ui-text-center ui-notif-count"  style="top:-10px;background:orange;border:1px solid orange">P</span>pending cheque/s</span>
		<span style="color:red;text-indent:30px;font-weight:bold;display: inline-block;font-size: 11px;text-transform: uppercase"><span class="ui-corner-all ui-text-center ui-notif-count"  style="top:-10px;background:red;border:1px solid red">O</span>overdue</span>
	</div>
	<div class="clear"></div>
</form>
<div id="cviewitem" title="Item List">
    <table style="height:200px;margin-top:5px;" id="clistitem">
        <thead>
            <tr style="display:block;position:relative;">
                <th style="width: 100px;">Barcode</th>
                <th style="width: 250px;" class="ui-text-left">Description</th>
                <th style="width: 100px;">Unit</th>
                <th style="width: 75px;">Qty</th>
                <th style="width: 100px;">Price</th>
                <th style="width: 100px;">Amount</th>
            </tr>
        </thead>
        <tbody style="height: 150px;overflow: auto;display: block;width:100%;">
        </tbody>
    </table>	
</div>
<div id="cviewpayment" title="Payment Log">
    <table style="height:300px;margin-top:5px;" id="clistpay" class="clistpay">
        <thead>
            <tr style="display:block;position:relative;">
                <th style="width: 80px;">Date</th>
                <th style="width: 170px;">Amount</th>
                <th style="width: 300px;">Info</th>
                <th style="width: 120px;">Status</th>
            </tr>
        </thead>
        <tbody style="height: 250px;overflow: auto;display: block;width:100%">
        </tbody>
    </table>
	<form class="form_style" style="width:40%;float:left;padding-left: 20%;">
		<input type="button" id="csavec"  value="Save Changes" disabled />
	</form>
	<table style="border:0px;width: 40%;float: right;">
		<thead>
			<tr>
				<td style="width:50%"></td>
				<td style="width:50%"></td>
			</tr>
		</thead>
		<tbody>
			<tr>
				<td>Total Balance :</td>
				<td class="ui-text-center">&#8369; <span id="ttotal_">0.00</span></td>
			</tr>
			<tr>
				<td>Remaining Balance :</td>
				<td class="ui-text-center">&#8369; <span id="rtotal_">0.00</span></td>
			</tr>
			<tr>
				<td>Total Amount Paid :</td>
				<td class="ui-text-center">&#8369; <span id="ptotal_">0.00</span></td>
			</tr>
		</tbody>
	</table>
</div>
<div style="position:absolute;width:2in;height:auto;background:#fff;display:none;" id="partial_receipt">
	<style tyle="text/css">
    <!--
        @page { size:5.5in 4in; margin: .38in; }
        .partial_receipt { width:5.5in; height:auto; padding: 20px;}
        .partial_receipt .ui-text-center { text-align:center;display:block;width:100%; }
        .partial_receipt, .partial_receipt table { font-size:12px; font-family: Verdana; }
        .partial_receipt small{ margin:0px;font-size: 12px; font-family: Verdana; }
        .partial_receipt .clear{ display:block;clear:both; }
        .partial_receipt .tdiscount{display:none;}
		.partial_receipt p span{display:inline-block;width:inherit;border-bottom:1px solid #000;text-indent:5px;text-align:center;font-size:15px}
		.partial_receipt p a{font-size:15px}
        td.b1{ border:1px solid #000; }
        td.b2{ border-bottom:1px solid #000; }
    -->
	</style>
	<div class="partial_receipt">
        <h4 class="ui-text-center" style="font-size: 12px;margin: 0px;">LVT GENERAL MERCHANDISE</h4>
        <small class="ui-text-center">General Market, Nalasin,</small>
        <small class="ui-text-center">Balaoan, La Union</small>
        <small class="ui-text-center">Ligaya B. Teofilo - Prop.</small>
        <small class="ui-text-center">639189650271</small>
		<br />
		<br />
        <span style="display:inline-block;font-weight:bold;">Official Receipt</span><span style="display:inline-block;float:right;">Date: <?php echo date("Y-m-d") ?></span>
		<br />
		<p style="text-indent:30px">Receive From <span style="width: 410px;" id="pname">&nbsp;</span></p>
		<p>Address <span style="width: 472px;" id="paddress">&nbsp;</span></p>
		<p style="padding-left: 80px;text-align:justify;line-height: 1.5;min-height:48px;text-align: center;">
			<a style="text-transform:capitalize;padding-right: 200px;"  id="pcashword"></a>
		</p>
		<p style="margin-top: -18;position: absolute;width: 528px;margin-top: -36px;">
			<span style="float: right;border: 0px;position: absolute;right: 0px;width: 185px;">
				(&#8369;<span style="width:150px" id="pcash">&nbsp;</span>)
			</span>
		</p>
		<p>as full/partial payment of Invoice No.<span style="width: 200px;" id="por">&nbsp;</span>.</p>
		<p style="margin:10px 0 0;">Total Sales : &#8369;<span style="width: 150px;" id="ptotal">&nbsp;</span></p>
		<p>Balance : &#8369;<span style="width: 170px;" id="pbalance">&nbsp;</span></p>
		<div style="width: 150px;float: right;clear: both;height: 15px;padding: 10px;margin-top: -50px;">NO.<span style="border:0px;">&nbsp;</span></div>
		<p style="clear:both;">
			<span style="border: 0px;border-top: 1px solid #000;float: right;text-align: center;width: 200px;margin-top:10px">Signature</span>
		</p>
		<p style="border-bottom: 1px solid;position: absolute;width: 433px;top: 208px;left: 121px;">&nbsp;</p>
		<p style="position: absolute;width: 433px;top: 208px;left: 27px;">the amount of </p>
		<p style="border-bottom: 1px solid;position: absolute;width: 350px;top: 230px;left: 28px;">&nbsp;</p>
	</div>
</div>
<script>
var dialogviewitem = function(){
    $('#cviewitem').dialog({
        autoOpen: false,
        height: 350,
        width: 700,
    	modal: true,
        resizable: false,
        hide: {
            effect: "fade",
            duration: 400
        }
    });
};
var dialogpayment = function(){
    $('#cviewpayment').dialog({
        autoOpen: false,
        height: 450,
        width: 700,
    	modal: true,
        resizable: false,
        hide: {
            effect: "fade",
            duration: 400
        }
    });
};
dialogpayment();
dialogviewitem();
$('#clist').click(function(){
	if($(this).attr('disabled'))
		return;
	$('#clistitem tbody').hide().html('');
	$.post('?module=pos&action=sales&action2=credit&ajax=1',{list:1,key:$(this).attr('iscid')},function(data){
    	$('#clistitem tbody').html(data).fadeIn();
		refunctr();
	});
	$('#cviewitem').dialog('open');
});
$('#cplist').click(function(){
	if($(this).attr('disabled'))
		return;
	$('#clistpay').attr('or', $('#cor').val().replace('#','').replace(/0/g,''));
	loadplist($(this).attr('iscid'));
	$('#csavec').attr('iscid', $(this).attr('iscid'));
	if($('.clistpay').length > 1)
		$('.clistpay').not(':first').remove();
	$('#cviewpayment').dialog('open');
});
$('.ccash').click(function(){$(this).select();})
.blur(function(){
	$('.ccash').val(formatCurrency($(this).val()));
}).keyup(function(){
	var change = toNumber($(this).val()) - toNumber($('.camountdue').val());
	if(change < 0)
		change = 0;
	$('.cchange').val(formatCurrency(change, true));
});
//.keypress(function(event) {if ( event.which == 13 ){$('#cpay').trigger('click');}});
$('#datepick1').datepicker({
    defaultDate: null,
    onClose: function( selectedDate ) {
      $('#datepick2').datepicker( "option", "minDate", selectedDate );
    }
});
$('#datepick2').datepicker({
    defaultDate: "+1w",
    onClose: function( selectedDate ) {
      $('#datepick1').datepicker( "option", "maxDate", selectedDate );
    }
});
$('#datepick1, #datepick2').datepicker( "option", "dateFormat", "yy-mm-dd" );
$('#datepick2').datepicker( "option", "minDate", $('#datepick1').val());
$('#datepick1').datepicker( "option", "maxDate", $('#datepick2').val() );
<?php $date = new DateTime(date("Y-m-01"));?>
$('#datepick1').val("<?php echo $date->format('Y-m-d'); ?>");
<?php $date->modify('+28 day'); ?>
$('#datepick2').val("<?php echo $date->format('Y-m-d'); ?>");
$('#datepick1, #datepick2').change(function(){
	var e = jQuery.Event("keypress");
	e.which = 13; //choose the one you want
	e.keyCode = 13;
	if($('#datepick2').val() != "")
		$('#datepick1').datepicker( "option", "maxDate", $('#datepick2').val() );
	if($('#datepick1').val() != "")
		$('#datepick2').datepicker( "option", "minDate", $('#datepick1').val() );
	$('#csearch').trigger(e);
});
var refunctr = function(){
    $('#ctable tbody tr').click(function(){
		//if($(this).find('td:last-child').html() == 'Paid')
		//	return;
    	$(this).parent().children().removeClass('trselected');
    	$(this).addClass('trselected');
    	$('#cor').val($(this).find('td:first-child').html());
    	$('#cname').val($(this).find('td:nth-child(2)').html());
		$('#cname').attr('address',$(this).attr('address'));
    	$('#cbalance').val(formatCurrency($(this).find('td:last-child').attr('value'), true));
    	$('.cbutton').attr('disabled', false).attr('iscid',$(this).attr('iscid'));
		var pending =  $(this).attr('pending')*1;
		var left = $(this).attr('left')*1;
		var delivered = $(this).attr('delivered');
		var balance = $(this).find('td:last-child').attr('value');
		
		if (balance == 0)
			mes = " -- "
		else if(left > 0)
			mes = $(this).attr('due') + " (" + left + " day/s left)";
		else if(left == 0)
			mes = "Today";
		else if(left == -1)
			mes = "Yesterday";
		else
			mes = "Overdue " + left*-1 + " day/s ago";
		$('input.adue').val('');
		if(left < 0){
			$('.adue').fadeIn();
			left = Math.floor(left / -30);
			if(left < 1)
				left = 1;
			var comp = toNumber($('#cbalance').attr('value'))*1;
			comp += comp * 5 * left / 100;
			$('input.adue').val(formatCurrency(comp, true));
		}else
			$('.adue').fadeOut();
		$('#cdue').val(mes);
		$('#ccdate').text($(this).attr('date'));
		
		if($('#cbalance').attr('value') == 0){
    		$('#cpay').attr('disabled', true);
    		$('#ccheque').attr('disabled', true);
		}
		if(pending > 0){
			$('#pending').fadeIn('slow').text(pending);
		}else{
			$('#pending').fadeOut();
		}
		if(delivered != "")
			$('#cdeliver').attr('delivered', delivered);
		else
			$('#cdeliver').attr('delivered', '');
    });
	 $('#clistitem tbody tr').each(function(){
	 	x = toNumber($(this).find('td:nth-child(6)').html());
		$(this).find('td:nth-child(6)').html('&#8369;'+formatCurrency(x,true));
	 	x = $(this).find('td:nth-child(5)').html();
		$(this).find('td:nth-child(5)').html(formatCurrency(x,true));
	 });
};
$('#cshow').selectBox().change( function() {
	var e = jQuery.Event("keypress");
	e.which = 13; //choose the one you want
	e.keyCode = 13;
	$('#csearch').trigger(e);
});
$('#csearch').keypress(function(event) {
	if ( event.which == 13 ) {
		key = $(this).val();
		date = $('#datepick1').val();
		date2 = $('#datepick2').val();
		display = $('#cshow').val();
		$.post('?module=pos&action=sales&action2=credit&ajax=1&searching=1',{date:date,date2:date2,key:key,display:display},function(data){
			$('#ctable tbody').hide().html(data).fadeIn('slow');
			$('#ctable tbody tr').each(function(){
				g = $(this).find('td:first-child').html();
				$(this).find('td:first-child').html(createOR(g));
				g = $(this).find('td:last-child').html();
				$(this).find('td:last-child').attr('value', g);
				if(g == 0)
					$(this).find('td:last-child').html('<span class="val ui-text-center">Paid</span>');
				else
					$(this).find('td:last-child').html('<span class="val ui-text-center">'+formatCurrency(g,true)+'</span>');
				
				if(toNumber($(this).attr('left')) < 0){
					var h = $(this).find('td:last-child');
					h.find('.val').css({'z-index': '1', 'position': 'relative', 'display': 'block', 'margin-bottom': '-15px', 'margin-top': '0px'});
					$(this).find('td:last-child').html(h.html() + '<span class="ui-corner-all ui-text-center ui-notif-count"  style="top:-15px;background:red;border:1px solid red;margin-right:2px;z-index:10;">O</span>');
				}
				if($(this).attr('pending') != "0"){
					var h = $(this).find('td:last-child');
					h.find('.val').css({'z-index': '1', 'position': 'relative', 'display': 'block', 'margin-bottom': '-15px', 'margin-top': '0px'});
					$(this).find('td:last-child').html(h.html() + '<span class="ui-corner-all ui-text-center ui-notif-count"  style="top:-15px;background:orange;border:1px solid orange;margin-right:2px;z-index:10;">P</span>');
				}
				if($(this).attr('delivered') != ""){
					var h = $(this).find('td:last-child');
					h.find('.val').css({'z-index': '1', 'position': 'relative', 'display': 'block', 'margin-bottom': '-15px', 'margin-top': '0px'});
					$(this).find('td:last-child').html(h.html() + '<span class="ui-corner-all ui-text-center ui-notif-count"  style="top:-15px;background:green;border:1px solid green;margin-right:2px;z-index:10;">D</span>');
				}
			});
			refunctr();
			clearForms();
		});
	}
});
var clearForms = function(){
    $('#cor').val('');
    $('#cname').val('');
    $('#cbalance').val('0.00');
    $('.cbutton').attr('disabled', true);
    $('#paycash').fadeOut('slow');
    $('#ccash').val('0.00');
    $('#ccdate').text('');
    $('#cdue').val('');
	$('.adue').fadeOut(); 
	$('#paycash').fadeOut();
	$('#paycheque').fadeOut();
	$('.ccash').val('0.00');
	$('.camountdue').val('0.00');
	$('#pending').fadeOut();
	$('#cdeliver').attr('delivered', '');
}
var createOR = function(or){
	max = 12;
  	ornum = '#';
  	max -= or.split('').length;
  	if (max < 0) max = 0;
  	for (u=0; u<max; u++)
  	{
  		ornum = ornum + '0';
  	}
  	return ornum + or;
};
$('#ccheque').click(function(){
	if($(this).attr('disabled') || $('#paycheque').css('display') != 'none')
		return;
	$('#paycash').fadeOut();
	$('#paycheque').fadeIn('slow').find('table').find('input').val('');
	
	$('.ccash').val('0.00');
	$('.cchange').val('0.00');
	$('.camountdue').val($('#cbalance').val());
	$('.issuepayment').attr('iscid', $(this).attr('iscid'));
	if(toNumber($('#cbdue').val())*1 > 0)
		$('.camountdue').val($('#cbdue').val());
		
	return;
});
$('#cpay').click(function(){
	if($(this).attr('disabled') || $('#paycash').css('display') != 'none')
		return;
	$('#paycheque').fadeOut();
	$('#paycash').fadeIn('slow');
	$('.ccash').val('0.00');
	$('.cchange').val('0.00');
	$('.camountdue').val($('#cbalance').val());
	$('.issuepayment').attr('iscid', $(this).attr('iscid'));
	if(toNumber($('#cbdue').val())*1 > 0)
		$('.camountdue').val($('#cbdue').val());
});

$('.checkinput').blur(function(){
	var max = toNumber($(this).attr('maxlength')) - $(this).val().length;
	var zero = "";
	if(isNaN($(this).val()))
		$(this).val('')
	else if($(this).val().length > 0){
		for(i=0; i<max ; i++)
			zero += "0";
		$(this).val(zero+$(this).val());
	}
}).click(function(){$(this).select();});

$('#cdeliver').click(function(){
    if($(this).attr('disabled'))
    	return;
	if($(this).attr('delivered') != ""){
		alert("Delivered on : " + $(this).attr('delivered'));
	}else{
        $.post('?module=pos&action=sales&action2=credit&ajax=1',{setdelivered:1, key:$(this).attr('iscid')},function(){	
              	var e = jQuery.Event("keypress");
              	e.which = 13; //choose the one you want
              	e.keyCode = 13;
              	$('#csearch').trigger(e);
        });
	}
});
	
$('.issuepayment').click(function(){
	var cash = toNumber($('.ccash').val());
	var check = "";
	var bank = $('#cbank').val();
	var id = $(this).attr('iscid');
	var or = $('#cor').val().replace('#','').replace(/0/g,'');
	var balance = toNumber($('#cbalance').attr('value'));
	
	$('.checkinput').each(function(){
		check += $(this).val() + "-";
	});
	
	if(cash < 1){
		alert("Insuficient Cash.");
		return;
	}
	if($(this).hasClass('check')){
    	if(check.length < 31){
    		alert("Invalid check nos.");
    		return;
    	}
    	if(bank.length == 0){
    		alert("Invalid Bank name.");
    		return;
    	}
	}
	check = check.substring(0,check.length-1);
	$.post('?module=pos&action=sales&action2=credit&ajax=1',{paying:1, cash:cash, check:check, bank:bank, key:id, balance:balance}, function(data){
    	var e = jQuery.Event("keypress");
    	e.which = 13; //choose the one you want
    	e.keyCode = 13;
    	$('#csearch').val(or).trigger(e).val('');
		console.log(data);
		$('#partial_receipt #pname').text($('#cname').val());
		$('#partial_receipt #pcashword').text(convertToWordCurrency(cash));
		$('#partial_receipt #pcash').text(formatCurrency(cash, true));
		$('#partial_receipt #ptotal').text(formatCurrency(data, true));
		balance = balance-cash;
		if(balance < 0)
			balance = 0;
		$('#partial_receipt #pbalance').text(formatCurrency(balance, true));
		$('#partial_receipt #por').text($('#cor').val().replace('#',''));
		$('#partial_receipt #paddress').html($('#cname').attr('address') + "&nbsp;");
		printDocument($('#partial_receipt').html());
	});
});

$('#csavec').click(function(){
    if($(this).attr('disabled'))
    	return;
	$('#clistpay tbody tr').each(function(){
		var status = $(this).find('td:last-child select').val();
		if(status == 'I' || status == 'V'){
        	$.post('?module=pos&action=sales&action2=credit&ajax=1',{setcheck:1, key:$(this).attr('isclid'), status:status},function(data){console.log(data);});
		}
  		if($(this).is(':last-child')){
  			var e = jQuery.Event("keypress");
      		e.which = 13; //choose the one you want
      		e.keyCode = 13;
      		var or = $('#clistpay').attr('or');
      		$('#csearch').val(or).trigger(e).val('');
  			loadplist($('#csavec').attr('iscid'));
  		}
	});
	$(this).attr('disabled', true);
});

var loadplist = function(id){
$.post('?module=pos&action=sales&action2=credit&ajax=1',{plist:1,key:id},function(data){
    	$('#clistpay tbody').html(data).fadeIn();
		var total_ = 0;
		var totalp = 0;
		$('#clistpay tbody tr').each(function(){
			var cash = $(this).find('td:nth-child(2)').text();
			var stat = $(this).find('td:nth-child(4)').text().trim();
			$(this).find('td:nth-child(2)').html('&#8369;' + formatCurrency(cash, true));
			if(stat.length == 0){
				$(this).find('td:nth-child(4)').html("<select style='width:inherit' default='P'><option value='P'>Pending</option><option value='I'>Invalid</option><option value='V'>Accept</option></select>");
			}else if(stat == 'Successful')
				totalp +=  toNumber($(this).attr('amount'));
			console.log(totalp);
			total_ = toNumber($(this).attr('interest'));
			if(total_ == 0)
				total_ = toNumber($(this).attr('balance'));
		});
		$('#ttotal_').html(formatCurrency(total_, true));
		remain = total_ - totalp;
		//if(remain < 0)
		//	remain = 0;
		$('#rtotal_').html(formatCurrency(remain, true));
		$('#ptotal_').html(formatCurrency(totalp, true));
		$('#clistpay tbody').find('select').selectBox().change( function() {
			var changes = false;
			$('#clistpay tbody').find('select').each(function(){
				if($(this).attr('default') != $(this).val() && !changes)
					changes = true;
			});
			$('#csavec').attr('disabled', !changes);
		});
	});
};	
$('#caddnew').click(function(){
	$.get('?module=login&action=search&ajax=1', function(data){
		$('#form_search_customer').html(data).dialog('open');
		$('#cussearch').attr('target', '#').trigger('keyup');
	});
});
</script>